package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.entity.DrawlingMark;
import cc.mrbird.febs.system.service.IDrawlingMarkService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author MrBird
 * @date 2019-07-20 23:16:03
 */
@Slf4j
@Validated
@Controller
public class DrawlingMarkController extends BaseController {

    @Autowired
    private IDrawlingMarkService drawlingMarkService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "drawlingMark")
    private String drawlingMarkIndex(){
        return FebsUtil.view("drawlingMark/drawlingMark");
    }

    @GetMapping("drawlingMark")
    @RequiresPermissions("drawlingMark:list")
    public FebsResponse getAllDrawlingMarks(DrawlingMark drawlingMark) {
        return new FebsResponse().success().data(drawlingMarkService.findDrawlingMarks(drawlingMark));
    }

    @GetMapping("drawlingMark/list")
    @RequiresPermissions("drawlingMark:list")
    public FebsResponse drawlingMarkList(QueryRequest request, DrawlingMark drawlingMark) {
        Map<String, Object> dataTable = getDataTable(this.drawlingMarkService.findDrawlingMarks(request, drawlingMark));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("新增DrawlingMark")
    @PostMapping("drawlingMark")
   // @RequiresPermissions("drawlingMark:add")
    @ResponseBody
    public FebsResponse addDrawlingMark(@Valid DrawlingMark drawlingMark) throws FebsException {
        try {
            this.drawlingMarkService.createDrawlingMark(drawlingMark);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增DrawlingMark失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除DrawlingMark")
    @GetMapping("drawlingMark/delete")
    @RequiresPermissions("drawlingMark:delete")
    public FebsResponse deleteDrawlingMark(DrawlingMark drawlingMark) throws FebsException {
        try {
            this.drawlingMarkService.deleteDrawlingMark(drawlingMark);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DrawlingMark失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改DrawlingMark")
    @PostMapping("drawlingMark/update")
    @RequiresPermissions("drawlingMark:update")
    public FebsResponse updateDrawlingMark(DrawlingMark drawlingMark) throws FebsException {
        try {
            this.drawlingMarkService.updateDrawlingMark(drawlingMark);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改DrawlingMark失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("drawlingMark/excel")
    @RequiresPermissions("drawlingMark:export")
    public void export(QueryRequest queryRequest, DrawlingMark drawlingMark, HttpServletResponse response) throws FebsException {
        try {
            List<DrawlingMark> drawlingMarks = this.drawlingMarkService.findDrawlingMarks(queryRequest, drawlingMark).getRecords();
            ExcelKit.$Export(DrawlingMark.class, response).downXlsx(drawlingMarks, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
